package org.csu.hotelsys.controller;

import org.csu.hotelsys.domain.Check_in;
import org.csu.hotelsys.domain.Room_Reserve;
import org.csu.hotelsys.service.FormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.annotation.SessionScope;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/form/")
@Controller
@SessionScope
public class FormController {
    @Autowired
    private FormService formService;

    SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");

    @GetMapping("viewAllCheckIn")
    public String viewAllCheckIn(Model model) {
        List<Check_in> check_inList = new ArrayList<>();
        check_inList = formService.getAllCheck_in();
        model.addAttribute("check_inList", check_inList);
        model.addAttribute("length", check_inList.size());
        System.out.println();
        return "form/check_in";
    }

    @GetMapping("viewAllRoomReserve")
    public String viewAllRoomReserve(Model model) {
        List<Room_Reserve> room_reserveList = new ArrayList<>();
        room_reserveList = formService.getAllRoom_Reserve();
        model.addAttribute("room_reserveList", room_reserveList);
        model.addAttribute("length", room_reserveList.size());
        System.out.println();
        return "form/room_reserve";
    }

    @GetMapping("viewAllCheckOut")
    public String viewAllCheckOut(Model model) {
        List<Check_in> check_outList = new ArrayList<>();
        check_outList = formService.getAllCheck_out();
        model.addAttribute("check_outList", check_outList);
        model.addAttribute("length", check_outList.size());
        System.out.println();
        return "form/check_out";
    }

    @GetMapping("viewCheckInByTime")//一段时间内的入住信息
    public String viewCheckInByTime(String start, String end, Model model) throws ParseException {
        if (!start.isEmpty() && !end.isEmpty()) {
            System.out.println("viewCheckInByTime");
            List<Check_in> check_inList = new ArrayList<>();
            Date startDate = dateformat.parse(start);
            Date endDate = dateformat.parse(end);
            System.out.println(dateformat.format(startDate));
            if (startDate.before(endDate)) {
                check_inList = formService.getCheck_in(dateformat.format(startDate), dateformat.format(endDate));
                model.addAttribute("length", check_inList.size());
                model.addAttribute("check_inList", check_inList);
                return "form/check_in";//回填需要跳转的界面
            } else {
                //否则返回所有的入住信息界面
                return "redirect:/form/viewAllCheckIn";
            }
        } else
            return "redirect:/form/viewAllCheckIn";
    }

    @GetMapping("viewCheckInByNow")//当天的入住信息
    public String viewCheckIn(Model model) throws ParseException {
        //不需要参数，直接获取当前时间
        List<Check_in> check_inList = new ArrayList<>();
        Date now = new Date();
        check_inList = formService.getCheck_in(dateformat.format(now));
        model.addAttribute("length", check_inList.size());
        model.addAttribute("check_inList", check_inList);
        return "form/check_in";//回填跳转的界面
    }


    @GetMapping("viewRoomReserveByTime")
    public String viewRoomReserve(String start, String end, Model model) throws ParseException {

        if (!start.isEmpty() && !end.isEmpty()) {
            List<Room_Reserve> room_reserveList = new ArrayList<>();
            Date startDate = dateformat.parse(start);
            Date endDate = dateformat.parse(end);
            if (startDate.before(endDate)) {
                room_reserveList = formService.getRoom_Reserve(dateformat.format(startDate), dateformat.format(endDate));
                model.addAttribute("room_reserveList", room_reserveList);
                model.addAttribute("length", room_reserveList.size());
                return "form/room_reserve";
            } else
                return "redirect:/form/viewAllRoomReserve";//返回所有预订界面
        }
        return "redirect:/form/viewAllRoomReserve";//返回所有预订界面
    }

    @GetMapping("viewRoomReserveByNow")
    public String viewReserve(Model model) {
        System.out.println("你好");
        //不需要参数，直接获取当前时间
        List<Room_Reserve> room_reserveList = new ArrayList<>();
        Date now = new Date();
        room_reserveList = formService.getRoom_Reserve(dateformat.format(now));
        model.addAttribute("room_reserveList", room_reserveList);
        model.addAttribute("length", room_reserveList.size());
        return "form/room_reserve";
    }

    @GetMapping("viewCheckOutByNow")
    public String viewCheckOut(Model model) {
        //不需要参数，直接获取当前时间
        List<Check_in> check_outList = new ArrayList<>();
        Date now = new Date();
        check_outList = formService.getCheck_out(dateformat.format(now));
        model.addAttribute("check_outList", check_outList);
        model.addAttribute("length", check_outList.size());
        return "form/check_out";//回填跳转的界面
    }

    @GetMapping("viewCheckOutByTime")
    public String viewCheckOutByTime(String start, String end, Model model) throws ParseException {
        List<Check_in> check_outList = new ArrayList<>();

        if (!start.isEmpty() && !end.isEmpty()) {
            Date startDate = dateformat.parse(start);
            Date endDate = dateformat.parse(end);
            if (startDate.before(endDate)) {
                check_outList = formService.getCheck_out(dateformat.format(startDate), dateformat.format(endDate));
                model.addAttribute("check_outList", check_outList);
                model.addAttribute("length", check_outList.size());
                return "form/check_out";
            } else
                return "redirect:/form/viewAllCheckOut";//返回所有预订界面
        }
        return "redirect:/form/viewAllCheckOut";//返回所有预订界面
    }

}
